/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.scalia.meteocal.control;

import com.scalia.meteocal.entity.Event;
import com.scalia.meteocal.entity.Notification;
import com.scalia.meteocal.entity.NotificationType;
import com.scalia.meteocal.entity.User;
import javax.ejb.Stateless;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author gabriele
 */
@Stateless
public class NotificationManager {

    @PersistenceContext
    EntityManager em;

    @EJB
    UserManager userManager;

    @EJB
    EventManager eventManager;

    public void eventDeleted(Event event) {
        List<User> partecipants = userManager.getAllPartecipantsToEvent(event);
        Date now = new Date();
        for (User partecipant : partecipants) {
            Notification notification = new Notification(NotificationType.DELETE_EVENT, partecipant, event, now);
            em.persist(notification);
        }
    }
    
    public void eventModified(Event event) {
        List<User> partecipants = userManager.getAllPartecipantsToEvent(event);
        Date now = new Date();
        for (User partecipant : partecipants) {
            Notification notification = new Notification(NotificationType.MODIFY_EVENT, partecipant, event, now);
            em.persist(notification);
        }
    }
    
    public void invitationsSent(Event event, User receiver) {
        Date now = new Date();
        Notification notification = new Notification(NotificationType.INVITATION, receiver, event, now);
        em.persist(notification);

    }
    
    public void badWeatherForOutdoorEvent(Event event) {
        List<User> partecipants = userManager.getAllPartecipantsToEvent(event);
        Date now = new Date();
        em.persist(new Notification(NotificationType.BAD_WEATHER, event.getCreator(), event, now));
        for (User partecipant : partecipants) {
            Notification notification = new Notification(NotificationType.BAD_WEATHER, partecipant, event, now);
            em.persist(notification);
        }
    }
    
    public List<Notification> getAllNotificationsOfUser(User user) {
        Query query = em.createNamedQuery(Notification.findAllOfUser).setParameter("user", user);
        return query.getResultList();
    }
    
    public List<Notification> getAllMyNotifications() {
        return getAllNotificationsOfUser(userManager.getLoggedUser());
    }
    
    public List<Notification> getAllNotificationsOfUserNewFirst(User user) {
        Query query = em.createNamedQuery(Notification.findAllOfUserNewFirst).setParameter("user", user);
        return query.getResultList();
    }
    
    public List<Notification> getAllMyNotificationsNewFirst() {
        return getAllNotificationsOfUserNewFirst(userManager.getLoggedUser());
    }
    
    public void readNotification(Notification notification) {
        notification.setRead(true);
        em.merge(notification);
    }
    
}
